﻿using System;
using System.Collections;
using System.Data;
using ZYCommon;

namespace TextEditor
{
    

    //对表KBSection的相关操作
    public class KBSection : ZYDBRecordBase
    {
        private bool bolShowInTree = true;
        private KB_List myKBList = null;
        private string strKBSEQ = "0000000000";
        private string strOwnerSection = "";

        //创建新纪录 覆盖父类
        public override ZYDBRecordBase CreateNewRecord()
        {
            return new KBSection();
        }

        //删除KBSection表中的记录
        public override bool CustomDelete(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Delete From KBSection Where KB_SEQ =? And OwnerSection=?";
            myCmd.Parameters.Clear();
            ZYDBConnection.AddParameter(myCmd, this.strKBSEQ);
            ZYDBConnection.AddParameter(myCmd, this.strOwnerSection);
            myCmd.ExecuteNonQuery();
            return true;
        }

        //从结果集中读取数据 覆盖了ZYKBRecorder中的方法
        public override bool FromReader(IDataReader myReader, ZYDBConnection conn)
        {
            if (myReader.FieldCount == 3)//判断当前数据行是否有三列
            {
                this.strKBSEQ = myReader.IsDBNull(0) ? null : myReader.GetString(0);
                this.strOwnerSection = myReader.IsDBNull(1) ? null : myReader.GetString(1);
                this.bolShowInTree = myReader.IsDBNull(2) || (myReader.GetString(2) != "0");
                return true;
            }
            if (myReader.FieldCount == 2)
            {
                this.strKBSEQ = myReader.IsDBNull(0) ? null : myReader.GetString(0);
                this.strOwnerSection = myReader.IsDBNull(1) ? null : myReader.GetString(1);
                return true;
            }
            return false;
        }

        //根据OwnerSection从KBSection表中查询KB_SEQ和OwnerSection 返回记录数
        public static int GetKBSectionRecords(ArrayList myList, ZYDBConnection myConn, string strSection)
        {
            if (StringCommon.isBlankString(strSection))
            {
                return 0;
            }
            strSection = strSection.Trim();
            using (IDbCommand myCmd = myConn.CreateCommand())
            {
                myCmd.CommandText = "Select KB_SEQ , OwnerSection From KBSection Where OwnerSection =?";
                ZYDBConnection.AddParameter(myCmd, strSection);
                KBSection myRecord = new KBSection();
                return myConn.ReadRecords(myList, myRecord, myCmd);
            }
        }

        //对KBSection表的查询语句
        public static string GetSelectSQL()
        {
            return "Select KB_SEQ , OwnerSection From KBSection ";
        }

        //表名
        public override string GetTableName()
        {
            return "KBSection";
        }

        public override bool isKeyEnable()
        {
            return true;
        }

        //根据KB_SEQ删除表KBSection中的记录
        public override bool SetDeleteCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Delete From KBSection Where KB_SEQ=?";
            ZYDBConnection.AddParameter(myCmd, this.strKBSEQ);
            return true;
        }

        //往表KBSection插入记录 并设置参数
        public override bool SetInsertCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Insert Into KBSection ( KB_SEQ , OwnerSection  ) Values(?,?)";
            myCmd.Parameters.Clear();
            ZYDBConnection.AddParameter(myCmd, this.strKBSEQ);
            ZYDBConnection.AddParameter(myCmd, this.strOwnerSection);
            return true;
        }

        //查询KBSection表中的所有记录
        public override bool SetSelectAllCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select KB_SEQ , OwnerSection  From KBSection ";
            return true;
        }

        //根据OwnerSection查询记录
        public override bool SetSelectCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            myCmd.CommandText = "Select KB_SEQ , OwnerSection  From KBSection Where OwnerSection = ?";
            ZYDBConnection.AddParameter(myCmd, this.strOwnerSection);
            return true;
        }

        public string KBSEQ
        {
            get
            {
                if (this.myKBList == null)
                {
                    return this.strKBSEQ;
                }
                return this.myKBList.SEQ;
            }
            set
            {
                this.strKBSEQ = value;
                if ((this.myKBList != null) && (this.myKBList.SEQ != value))
                {
                    this.myKBList = null;
                }
                base.SetModified();
            }
        }

        public string OwnerSection
        {
            get
            {
                return this.strOwnerSection;
            }
            set
            {
                this.strOwnerSection = value;
                base.SetModified();
            }
        }

        public bool ShowInTree
        {
            get
            {
                return this.bolShowInTree;
            }
            set
            {
                this.bolShowInTree = value;
            }
        }
    }
}

